import axios from 'axios'
export default {
      data() {
        return {
          tableData: [{
            create_time:'create_time',
            username: 'username',
            address: '挺好',
            email:'email',
            mobile:'mobile',
            handle:'没干嘛'
          }],
          //分页获取数据
          total:0,
          //获取当前页
          currentpage:0,
           input1:'',
           dialogFormVisible:false,
          form:{
            username:'',
            password:'',
            mobile:'',
            email:''
          },
          rules: {
            username: [
              { required: true, message: "请输入用户名称", trigger: "blur" },
              { min: 3, max: 6, message: "长度在 3 到 6个字符", trigger: "blur" },
            ],
            password: [
              { required: true, message: "请输入密码", trigger: "blur" },
              { min: 3, max: 12, message: "长度在 3到 12 个字符", trigger: "blur" },
            ],
            email: [
              { patten:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,message: "请输入正确的格式", trigger: "blur" },
            ],
            mobile: [
              { required: true, message: "请输入手机号", trigger: "blur" },
              { min: 8, max: 12, message: "长度在 8到 12 个数字", trigger: "blur" },
            ],
          }
        }
      },
      created(){
        this.loaderUser()
      },
      methods:{
        async loaderUser(pagenum=1,query=''){
          // axios.get('http://localhost:8888/api/private/v1/users',{
          //   params:{
          //     query,// 查询参数query:''是获取全部 query:key 获取为key的数据
          //     pagenum,//第一页
          //     pagesize:2//第一页的六个数据
          //   },headers:{
          //     Authorization:localStorage.getItem('token')
          //     //把令牌发送到后台确认
          //   }
          // }).then(res=>{
          //   this.tableData=res.data.data.users
          //   this.total=res.data.data.total
          //   this.currentpage=res.data.data.pagenum
          // })
          //改造axios
          let url='http://localhost:8888/api/private/v1/users';
          let config={
            params:{
                  query,// 查询参数query:''是获取全部 query:key 获取为key的数据
                  pagenum,//第一页
                  pagesize:2//第一页的六个数据
                },headers:{
                  Authorization:localStorage.getItem('token')
                  //把令牌发送到后台确认
                }
          }
          let res=await axios.get(url,config)
             this.tableData=res.data.data.users
            this.total=res.data.data.total
            this.currentpage=res.data.data.pagenum



        },
        currentchange(pug){
          //current-change翻页触发事件自动获取页数值
          console.log(pug)
          this.loaderUser(pug,this.input1)//需传下搜索的内容
        },
        query(){
          console.log(this.input1)
          this.loaderUser(1,this.input1)
        },
        async addform(){
          let res=await axios.post('http://localhost:8888/api/private/v1/users',this.form,{
            headers:{
              Authorization:localStorage.getItem('token')
            }
          })
            if(res.data.meta.status==201){
              this.dialogFormVisible=false

            }

        }

      }

    }

